﻿using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;
using LTJ.FrameWork;

namespace LTJ.WebPage.UserManage.user
{
    /// <summary>
    /// 企业用户编辑
    /// </summary>
    public partial class corpuseredit : UserPage
    {
        protected string opmode = SASRequest.GetString("opmode");
        protected int uid = SASRequest.GetInt("uid", 0);
        protected int gid = SASRequest.GetInt("gid", 0);
        protected UserInfo uinfo = new UserInfo();
        protected int testcount = 0;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void edituser_Click(object sender, EventArgs e)
        {
            uinfo.gender = SASRequest.GetInt("gender", 0);
            uinfo.status = SASRequest.GetInt("status", 1);
            uinfo.cid = SASRequest.GetInt("ajaxcorpdatalist_value", 0);
            uinfo.rname = rname.Text.Trim();
            uinfo.ename = ename.Text.Trim();
            uinfo.phone = phone.Text;
            uinfo.bio = Utils.RemoveHtml(bio.Text.Trim());

            if (opmode == "add")
            {
                if (Users.GetUserId(uname.Text.Trim()) > 0)
                {
                    base.RegisterStartupScript("PAGETEMPLATE", "该用户名已被注册，请填写其他用户名！");
                    return;
                }

                uinfo.gid = gid;
                uinfo.creater = username;
                uinfo.source = 1;
                uinfo.ip = Utils.GetRealIP();
                uinfo.lname = uname.Text;
                uinfo.syspass = Utils.getPassword();
                uinfo.pass = Utils.MD5(uinfo.syspass);

                int newuid = Users.CreateUser(uinfo);

                if (newuid <= 0)
                {
                    base.RegisterStartupScript("PAGETEMPLATE", "用户注册失败，请与管理员联系！");
                    return;
                }
                if (gid == Convert.ToInt16(ManageGroup.CorpGroup))
                {
                    if (!Users.SetCorpManagerAuth(companyinfo.id, newuid, false, ""))
                    {
                        base.RegisterStartupScript("PAGETEMPLATE", "权限设置出错，请与管理员联系！");
                        return;
                    }
                }
            }
            else if (opmode == "edit")
            {
                if (!Users.UpdateUser(uinfo))
                {
                    base.RegisterStartupScript("PAGETEMPLATE", "用户编辑失败，请与管理员联系！");
                    return;
                }
            }

            base.RegisterStartupScript("PAGE", "parent.document.getElementById('reclick').click();parent.Lightview.hide();");
        }

        #region Web 窗体设计器生成的代码

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            if (opmode == "edit")
            {
                uinfo = Users.GetUser(SASRequest.GetInt("uid", 0));
                rname.Text = uinfo.rname;
                ename.Text = uinfo.ename;
                phone.Text = uinfo.phone;
                bio.Text = uinfo.bio;
                uname.Visible = false;
            }

            if (opmode == "add")
            {
                int usercount = Users.GetUsersByCidGid(companyinfo.id, gid);
                testcount = usercount;
                if (gid == Convert.ToInt16(ManageGroup.CorpGroup))
                {
                    if (usercount >= companyinfo.mcount && companyinfo.mcount != -1)
                    {
                        base.RegisterStartupScript("", "<script>alert('管理员数量已达最大限制，不能进行添加操作!');parent.Lightview.hide();</script>");
                        return;
                    }
                }
                else if (gid == Convert.ToInt16(ManageGroup.NormalGroup))
                {
                    if (usercount >= companyinfo.ucount && companyinfo.ucount != -1)
                    {
                        base.RegisterStartupScript("", "<script>alert('用户数量已达最大限制，不能进行添加操作!');parent.Lightview.hide();</script>");
                        return;
                    }
                }
                else
                {
                    base.RegisterStartupScript("", "<script>alert('参数传递错误!');parent.Lightview.hide();</script>");
                    return;
                }
            }

            edituser.Click += new EventHandler(edituser_Click);
        }

        #endregion
    }
}